//
//  ZXAccount.m
//  ZXOCWeiBo
//
//  Created by mac on 15/12/4.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXAccount.h"

 static ZXAccount *account = nil;

@implementation ZXAccount
/**
 *  单例
 *
 *  @return 用户登录信息
 */
+(instancetype)shareAccount
{
    if (!account)
    {
        account = [[self alloc]init];
    }
    return account;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
   
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
#warning 登录账号数据初始化在allocWithZone方法
        account = [super allocWithZone:zone];
        //从沙盒取出登录数据
        NSUserDefaults * defailts = [NSUserDefaults standardUserDefaults];
       account.access_token = [defailts objectForKey:@"access_token"];
        account.remind_in = [defailts objectForKey:@"remind_in"];
        account.expires_in = [defailts objectForKey:@"expires_in"];
        account.uid = [defailts objectForKey:@"uid"];
    });
    
    return account;
}

/**
 *  将Accesstoken保存在沙盒中
 */
-(void)saveAccountToSanBox
{
    //获得偏好设置，
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.access_token forKey:@"access_token"];
    [defaults setObject:self.remind_in forKey:@"remind_in"];
    [defaults setObject:self.expires_in forKey:@"expires_in"];
    [defaults setObject:self.uid forKey:@"uid"];
    //同步
    [defaults synchronize];
}

@end
